package aooO.crm.Controller;

import aooO.crm.Service.*;
import aooO.crm.common.Utils.DateUtils;
import aooO.crm.common.Utils.UUIDUtils;
import aooO.crm.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("workbench/clue")
public class clueController {

    @Autowired
    DicValueService dicValueService;

    @Autowired
    UserService userService;

    @Autowired
    ClueService clueService;

    @Autowired
    ClueRemarkService clueRemarkService;

    @Autowired
    ActivityService activityService;

    @RequestMapping("/index.do")
    public String index(HttpServletRequest request){
        //在此之前需要查询的内容
        //线索来源 称呼 线索状态
        List<User> userList = userService.selectAllUser();
        List<DicValue> appellationList=dicValueService.queryDicValueByTypeCode("appellation");
        List<DicValue> sourceList=dicValueService.queryDicValueByTypeCode("source");
        List<DicValue> clueStateList=dicValueService.queryDicValueByTypeCode("clueState");

//        存到请求域
        request.setAttribute("userList",userList);
        request.setAttribute("appellationList",appellationList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("clueStateList",clueStateList);
        return "/workbench/clue/index";
    }

    @RequestMapping("/queryClueByConditionForPage.do")
    @ResponseBody
    public Object queryClueByConditionForPage(String fullname, String company, String phone, String source,
                                              String owner, String mphone, String state, int pageNo, int pageSize){
        /**
         * fullname:fullname,
         * 				company:company,
         * 				phone:phone,
         * 				source:source,
         * 				owner:owner,
         * 				mphone:mphone,
         * 				state:state,
         * 				pageNo:pageNo,
         * 				pageSize:pageSize
         */

        HashMap<String,Object> map=new HashMap<>();
        map.put("fullname",fullname);
        map.put("company", company);
        map.put("phone", phone);
        map.put("source", source);
        map.put("owner", owner);
        map.put("mphone", mphone);
        map.put("state", state);
        int beginNo=(pageNo - 1) * pageSize;
        map.put("beginNo", beginNo);   //开始页
        map.put("pageSize", pageSize);

        List<Clue> clueList = clueService.queryClueByConditionForPage(map);
        int totalRows = clueService.selectTotalPages(map);
        HashMap<String,Object> date =new HashMap<>();
        date.put("clueList",clueList);
        date.put("totalRows",totalRows);
        return date;
    }


//    workbench/clue/saveCreateClue.do

    /**
     * fullname:fullname,
     * 					appellation:appellation,
     * 					owner:owner,
     * 					company:company,
     * 					job:job,
     * 					email:email,
     * 					phone:phone,
     * 					website:website,
     * 					mphone:mphone,
     * 					state:state,
     * 					source:source,
     * 					description:description,
     * 					contactSummary:contactSummary ,
     * 					nextContactTime:nextContactTime,
     * 					address:address
     * @return
     */
    @RequestMapping("/saveCreateClue.do")
    @ResponseBody
    public Object saveCreateClue(Clue clue,HttpSession session){
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        clue.setId(UUIDUtils.uuid());
        clue.setCreateTime(DateUtils.FormatDateTime(new Date()));
        User use = (User) session.getAttribute(stateCode.SESSION_USER);
        clue.setCreateBy(use.getId());
        try{
            int i = clueService.CreateClue(clue);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
            }else {
                msg.setMessage("保存失败，请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("保存失败，请重试！");
        }
        return msg;
    }

//    workbench/clue/queryClueById.do
    @RequestMapping("/queryClueById.do")
    @ResponseBody
    public Object queryClueById(String id){
        Clue clue = clueService.queryClueById(id);
        return clue;
    }


//    workbench/clue/deleteClueByIds.do
    @RequestMapping("/deleteClueByIds.do")
    @ResponseBody
    public Object deleteClueByIds(String[] id){
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        try{
            int i = clueService.deleteClueByIds(id);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
            }else {
             msg.setMessage("保存失败，请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("保存失败，请重试！");
        }
        return msg;
    }

//    workbench/clue/saveEditClue.do
    @RequestMapping("/saveEditClue.do")
    @ResponseBody
    public Object saveEditClue(Clue clue,HttpSession session){
        returnObject msg=new returnObject(stateCode.STATE_FAILURE,"");
        User user = (User) session.getAttribute(stateCode.SESSION_USER);
        clue.setEditBy(user.getId());
        clue.setEditTime(DateUtils.FormatDateTime(new Date()));
        try{
            int i = clueService.updateClue(clue);
            if (i>0){
                msg.setCode(stateCode.STATE_SUCCESS);
            }else {
                msg.setMessage("保存失败，请重试！");
            }
        }catch (Exception e){
            e.printStackTrace();
            msg.setMessage("保存失败，请重试！");
        }
        return msg;
    }


//    workbench/clue/detailClue.do
    @RequestMapping("/detailClue.do")
    public String detailClue(String id,HttpServletRequest request){
        Clue clue = clueService.selectDetailClueById(id);
        //查询所有的评论信息
        List<ClueRemark> clueRemarkList = clueRemarkService.selectAllByClueId(id);
        //查询所有关联的市场活动
        List<Activity> activityList = activityService.selectAllActivityByClueId(id);
        request.setAttribute("clue",clue);
        request.setAttribute("clueRemarkList",clueRemarkList);
        request.setAttribute("activityList",activityList);
        return "workbench/clue/detail";
    }

}
